<template>
	<view class="agreement-container">
		<view class="modal-overlay">
			<view class="modal-content">
				<view class="modal-header">
					<text class="title">服务协议与隐私政策</text>
					<text class="subtitle">请您仔细阅读以下协议</text>
				</view>
				
				<view class="modal-body">
					<text class="description">
						为了更好地保护您的权益，请您阅读并同意《用户协议》和《隐私政策》。
						我们将严格按照相关法律法规保护您的个人信息安全。
					</text>
					
					<view class="agreement-links">
						<text class="link" @tap="openPrivacy">《隐私政策》</text>
						<text class="separator">和</text>
						<text class="link" @tap="openTerms">《用户协议》</text>
					</view>
					
					<view class="checkbox-section">
						<view class="checkbox-item" @tap="toggleAgreement">
							<view class="checkbox" :class="{ checked: isAgreed }">
								<text class="checkmark" v-if="isAgreed">✓</text>
							</view>
							<text class="checkbox-text">我已阅读并同意上述协议</text>
						</view>
					</view>
				</view>
				
				<view class="modal-footer">
					<button class="btn-disagree" @tap="disagree">不同意</button>
					<button class="btn-agree" :class="{ disabled: !isAgreed }" @tap="agree">同意</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isAgreed: false
			}
		},
		methods: {
			toggleAgreement() {
				this.isAgreed = !this.isAgreed
			},
			openPrivacy() {
				uni.navigateTo({
					url: '/pages/agreement/privacy'
				})
			},
			openTerms() {
				uni.navigateTo({
					url: '/pages/agreement/terms'
				})
			},
			disagree() {
				uni.showModal({
					title: '提示',
					content: '不同意协议将无法使用应用，确定要退出吗？',
					success: (res) => {
						if (res.confirm) {
							// 在uni-app中，可以使用plus.runtime.quit()退出应用
							// 但在H5环境中不支持，这里使用返回上一页的方式
							uni.navigateBack({
								delta: 1
							})
						}
					}
				})
			},
			agree() {
				if (!this.isAgreed) {
					uni.showToast({
						title: '请先勾选同意协议',
						icon: 'none'
					})
					return
				}
				
				try {
					// 保存用户同意状态到本地存储
					uni.setStorageSync('agreement_accepted', true)
					uni.setStorageSync('agreement_time', new Date().getTime())
					
					// 跳转到首页
					uni.reLaunch({
						url: '/pages/index/index'
					})
				} catch (e) {
					console.error('保存协议状态失败:', e)
					uni.showToast({
						title: '保存失败，请重试',
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style scoped>
	.agreement-container {
		width: 100vw;
		height: 100vh;
		background: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 40rpx;
		box-sizing: border-box;
	}
	
	.modal-overlay {
		width: 100%;
		max-width: 600rpx;
	}
	
	.modal-content {
		background: white;
		border-radius: 24rpx;
		overflow: hidden;
		box-shadow: 0 12rpx 40rpx rgba(0, 0, 0, 0.15);
	}
	
	.modal-header {
		padding: 50rpx 40rpx 30rpx;
		text-align: center;
		background: linear-gradient(135deg, #F5F7FA 0%, #C3CFE2 100%);
	}
	
	.title {
		font-size: 36rpx;
		font-weight: 600;
		color: #333333;
		display: block;
		margin-bottom: 16rpx;
	}
	
	.subtitle {
		font-size: 26rpx;
		color: #666666;
		display: block;
	}
	
	.modal-body {
		padding: 40rpx;
	}
	
	.description {
		font-size: 28rpx;
		color: #666666;
		line-height: 1.6;
		margin-bottom: 30rpx;
		display: block;
	}
	
	.agreement-links {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-bottom: 40rpx;
		flex-wrap: wrap;
	}
	
	.link {
		color: #4CAF50;
		font-size: 28rpx;
		text-decoration: underline;
		font-weight: 500;
	}
	
	.separator {
		color: #666666;
		font-size: 28rpx;
		margin: 0 10rpx;
	}
	
	.checkbox-section {
		margin-bottom: 20rpx;
	}
	
	.checkbox-item {
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.checkbox {
		width: 36rpx;
		height: 36rpx;
		border: 2rpx solid #E0E0E0;
		border-radius: 6rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-right: 16rpx;
		transition: all 0.3s ease;
	}
	
	.checkbox.checked {
		background-color: #4CAF50;
		border-color: #4CAF50;
	}
	
	.checkmark {
		color: white;
		font-size: 24rpx;
		font-weight: bold;
	}
	
	.checkbox-text {
		font-size: 26rpx;
		color: #666666;
	}
	
	.modal-footer {
		padding: 30rpx 40rpx 40rpx;
		display: flex;
		gap: 20rpx;
	}
	
	.btn-disagree {
		flex: 1;
		background: #F5F5F5;
		color: #666666;
		border: none;
		border-radius: 50rpx;
		padding: 28rpx 0;
		font-size: 32rpx;
		font-weight: 500;
	}
	
	.btn-agree {
		flex: 1;
		background: linear-gradient(135deg, #4CAF50, #81C784);
		color: white;
		border: none;
		border-radius: 50rpx;
		padding: 28rpx 0;
		font-size: 32rpx;
		font-weight: 500;
		box-shadow: 0 4rpx 12rpx rgba(76, 175, 80, 0.3);
	}
	
	.btn-agree.disabled {
		background: #E0E0E0;
		color: #999999;
		box-shadow: none;
	}
</style>